import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import nlsPlugin, { Languages, esbuildPluginMonacoEditorNls } from './vite-plugins/nls.js'
import zh_hans from './vite-plugins/zh.json'
import { fileURLToPath, URL } from 'node:url'


const plugins = [vue()]
plugins.push(nlsPlugin({
  locale: Languages.zh_hans,
  localeData: zh_hans,
}))

// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  
  const env = loadEnv(mode, process.cwd(), '')
  const target = env.VITE_API_TARGET || 'http://192.168.1.168:8080'
  // const target = env.VITE_API_TARGET || 'http://localhost:8080'

  return {
    plugins: [vue()],
    server: {
      proxy: {
        '/api': {
          target,
          changeOrigin: true,
        },
      },
    },
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
      },
    },

  optimizeDeps: {
    esbuildOptions: {
      plugins: [
        // 开发环境下通过esbuild插件进行汉化
        esbuildPluginMonacoEditorNls({
          locale: Languages.zh_hans,
          localeData: zh_hans,
        }),
      ],
    },
  },
  }
})
